/**
 * Copyright (c) 2020 lizebang
 *
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

package cmd

import (
	"os"

	"gitee.com/banggua/ggtsync/config"
	"gitee.com/banggua/ggtsync/handler"
	"gitee.com/banggua/ggtsync/logger"
	"gitee.com/banggua/ggtsync/plugin"
)

const (
	testSuccess = `%s: the configuration file %s syntax is ok
%s: the configuration file %s test is successful
`
	testFailure = `%s: %v
%s: the configuration file %s test failed
`
)

func test(filename string) error {
	err := config.Load(filename)
	if err != nil {
		handler.Templet()
		plugin.Templet()
		return err
	}
	return handler.Test(config.GetProjects())
}

func Test(filename string) {
	err := test(filename)
	if err != nil {
		logger.Errorf(testFailure, execname, err, execname, filename)
		os.Exit(1)
	}
	logger.Infof(testSuccess, execname, filename, execname, filename)
	os.Exit(0)
}
